//Shkolnij Rostislav
package CalculationCore;

public class CCPUReturnStack {
	private int vecR[];
	private boolean IsRealMode;
	
	
	public CCPUReturnStack(int depth)
	{
		int iDepth = depth;
		if(iDepth > 0)
		{
			vecR = new int[iDepth];
			for(int i=0; i<iDepth; i++)
				vecR[i] = 0;
			
			if(iDepth == 5)
				IsRealMode = true;
			else
				IsRealMode = false;
		}
		else 
			System.out.println(" !!! depth must be > 0");
	}
	
	public CCPUReturnStack(int[] src)
	{
		int iDepth = src.length;
		if(iDepth > 0)
		{
			vecR = new int[iDepth];
			for(int i=0; i<iDepth; i++)
				vecR[i] = src[i];
			
			if(iDepth == 5)
				IsRealMode = true;
			else
				IsRealMode = false;
		}
		else 
			System.out.println(" !!! depth must be > 0");
	}

	public int getSize()
	{
		return this.vecR.length;
	}
	public int getRbyIndex(int index)
	{
		return vecR[index];
	}
	
	public int[] getIntArray() {
		int[] res = new int[vecR.length];
		
		for(int i=0; i<vecR.length; i++)
			res[i] = vecR[i];
		return res;
	}
	
	public void setIntArray(int[] src) {
		if(src.length != vecR.length){
			System.out.println(String.format(" !!! Src length must be = %d", vecR.length));
		} else {
			for(int i=0; i<vecR.length; i++)
				vecR[i] = src[i];
		}
	}
	
	public boolean getIsRealMode() {
		return IsRealMode;
	}
	
	public void regPush(int src)	{
		int temp = vecR[0];
		int temp1;
		
		vecR[0] = src;
		for(int i=0; i<getSize()-1; i++) 
		{
			temp1 = vecR[i+1];
			vecR[i+1] = temp;
			temp = temp1;
		}//last temp1 is lost
	}
	public int regPop()	{
		int temp = vecR[getSize()-1];
		int temp1;
		vecR[getSize()-1] = 0;
		
		for(int i=getSize()-2; i>=0; i--) 
		{
			temp1 = vecR[i];
			vecR[i] = temp;
			temp = temp1;
		}
		
		return temp;
	}
	public void systemOutput(char c)
	{
		for(int i=0; i<getSize(); i++) 
		{
			System.out.println(String.format("%c%d : %2d", c, i, vecR[i]));
		}
	}
	
	public void cleanup()	{

		for(int i=0; i<this.vecR.length; i++)
			this.vecR[i] = 0;
	}
	
	public boolean compareAllRegisters(int[] src)
	{
		boolean res = true;
		if(src.length == this.vecR.length)	{
			
			for(int i = 0; i<src.length && res; i++) {
				res = (this.vecR[i] == src[i])? true : false;
			}
			return res; 
		} else {
			System.out.println(String.format(" !!! Src length must be = %d", this.vecR.length));
			return false; 
		}
	}

}
